/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * General.java
 *
 * Created on 19/05/2011, 14:11:57
 */
package vistas;

import Renderers.CellRenderer;
import Renderers.LabelRenderer;
import comu.Utilitats;
import controlador.AbonatControlador;
import controlador.CampControlador;
import controlador.CollitaControlador;
import controlador.ProduccioControlador;
import controlador.ProductorControlador;
import controlador.TractamentControler;
import controlador.autogenerats.exceptions.IllegalOrphanException;
import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Abonat;
import entity.Camp;
import entity.Collita;
import entity.Produccio;
import entity.Productor;
import entity.Tractament;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import vistas.dialogs.DialogSituarParcela;
import vistas.textImg.Textos;

/**
 *
 * @author David
 */
public class General extends javax.swing.JFrame implements Observer {

    Menu menu;
    Plantar plantar;
    Collir collir;
    Abonar abonar;
    Tractar tractar;
    ProductorControlador productors;
    CampControlador camps;
    ProduccioControlador produccions;
    CollitaControlador collites;
    AbonatControlador abonats;
    TractamentControler tractaments;
    TreePath lastPath;

    /** Creates new form General */
    public General(Menu menu) {
        this.menu = menu;
        productors = new ProductorControlador();
        camps = new CampControlador();
        produccions = new ProduccioControlador();
        collites = new CollitaControlador();
        abonats = new AbonatControlador();
        tractaments = new TractamentControler();
        initComponents();
        Utilitats.centrarVentana(this);
        update_Tree();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgVisualitzar = new javax.swing.ButtonGroup();
        cbProductor = new javax.swing.JComboBox();
        lbProductor = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();
        btMenu = new javax.swing.JButton();
        rbTot = new javax.swing.JRadioButton();
        rbSenseAcabar = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        taDescripcio = new javax.swing.JTextArea();
        lbMapa = new javax.swing.JLabel();
        btModificar = new javax.swing.JButton();
        btEliminar = new javax.swing.JButton();
        tbtAcabada = new javax.swing.JToggleButton();
        btAbonar = new javax.swing.JButton();
        btPlantar = new javax.swing.JButton();
        btCollir = new javax.swing.JButton();
        btRuixar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(Textos.GENERAL_TITOL);

        cbProductor.setModel(Utilitats.getComboBoxModel(productors.findProductorEntities()));
        cbProductor.setRenderer(new LabelRenderer());
        cbProductor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbProductorActionPerformed(evt);
            }
        });

        lbProductor.setText(Textos.GENERAL_LABEL_PRODUCTOR);

        tree.setAutoscrolls(true);
        tree.setCellRenderer(new CellRenderer());
        tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(tree);

        btMenu.setText(Textos.COMU_BOTO_MENU);
        btMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMenuActionPerformed(evt);
            }
        });

        bgVisualitzar.add(rbTot);
        rbTot.setText(Textos.GENERAL_CHEC_TOTS);
        rbTot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbTotActionPerformed(evt);
            }
        });

        bgVisualitzar.add(rbSenseAcabar);
        rbSenseAcabar.setSelected(true);
        rbSenseAcabar.setText(Textos.GENERAL_CHEC_SENSE_ACABAR);
        rbSenseAcabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSenseAcabarActionPerformed(evt);
            }
        });

        taDescripcio.setColumns(20);
        taDescripcio.setEditable(false);
        taDescripcio.setRows(5);
        jScrollPane2.setViewportView(taDescripcio);

        lbMapa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbMapaMouseClicked(evt);
            }
        });

        btModificar.setText(Textos.GENERAL_BOTO_MODIFICAR);
        btModificar.setEnabled(false);
        btModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btModificarActionPerformed(evt);
            }
        });

        btEliminar.setText(Textos.GENERAL_BOTO_ELIMINAR);
        btEliminar.setEnabled(false);
        btEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEliminarActionPerformed(evt);
            }
        });

        tbtAcabada.setText(Textos.GENERAL_BOTO_ACABADA);
        tbtAcabada.setEnabled(false);
        tbtAcabada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbtAcabadaActionPerformed(evt);
            }
        });

        btAbonar.setText(Textos.GENERAL_BOTO_ABONAR);
        btAbonar.setEnabled(false);
        btAbonar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAbonarActionPerformed(evt);
            }
        });

        btPlantar.setText(Textos.GENERAL_BOTO_PLANTAR);
        btPlantar.setEnabled(false);
        btPlantar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPlantarActionPerformed(evt);
            }
        });

        btCollir.setText(Textos.GENERAL_BOTO_COLLIR);
        btCollir.setEnabled(false);
        btCollir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCollirActionPerformed(evt);
            }
        });

        btRuixar.setText(Textos.GENERAL_BOTO_RUIXAR);
        btRuixar.setEnabled(false);
        btRuixar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRuixarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 467, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(7, 7, 7)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(btPlantar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(btModificar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(btCollir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(btAbonar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(tbtAcabada, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                                    .add(btEliminar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(51, 51, 51)
                                        .add(btMenu, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .add(btRuixar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, lbMapa, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)))
                            .add(layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))))
                    .add(layout.createSequentialGroup()
                        .add(lbProductor)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cbProductor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 191, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(rbSenseAcabar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(rbTot)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbProductor)
                    .add(cbProductor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(rbSenseAcabar)
                    .add(rbTot))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 215, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(btModificar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btPlantar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btCollir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(lbMapa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 157, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(7, 7, 7)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(btAbonar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btRuixar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(tbtAcabada, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(12, 12, 12)
                                .add(btMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btEliminar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE))))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbProductorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbProductorActionPerformed
        update_Tree();
}//GEN-LAST:event_cbProductorActionPerformed

    private void btMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMenuActionPerformed
        if (menu != null) {
            menu.setVisible(true);
            this.setVisible(false);
        }
}//GEN-LAST:event_btMenuActionPerformed

    private void rbTotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbTotActionPerformed
        update_Tree();
    }//GEN-LAST:event_rbTotActionPerformed

    private void rbSenseAcabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSenseAcabarActionPerformed
        update_Tree();
    }//GEN-LAST:event_rbSenseAcabarActionPerformed

    private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeValueChanged
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (node != null) {
            Object value = node.getUserObject();
            String out = "";
            lbMapa.setIcon(null);
            if (Camp.class.isInstance(value)) {
                //**********************************************************  CAMP  *************************
                Camp camp = (Camp) value;
                out = Utilitats.getDescripcio(camp);
                btModificar.setEnabled(false);
                btPlantar.setEnabled(true);
                btCollir.setEnabled(false);
                btAbonar.setEnabled(false);
                btRuixar.setEnabled(false);
                tbtAcabada.setEnabled(false);
                btEliminar.setEnabled(false);
                try {
                    Utilitats.adabtarImatgeAlLabel(lbMapa, camp.getImg());
                } catch (IOException ex) {
                    Logger.getLogger(General.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (Produccio.class.isInstance(value)) {
                //**********************************************************  PRODUCCIO  *************************
                Produccio produccio = (Produccio) value;
                out = Utilitats.getDescripcio(produccio);
                btModificar.setEnabled(true);
                btPlantar.setEnabled(false);
                tbtAcabada.setEnabled(true);
                boolean acabada = produccio.getTerminado();
                tbtAcabada.setSelected(acabada);
                if (!acabada) {
                    btCollir.setEnabled(true);
                    btAbonar.setEnabled(true);
                    btRuixar.setEnabled(true);
                } else {
                    btCollir.setEnabled(false);
                    btAbonar.setEnabled(false);
                    btRuixar.setEnabled(false);
                }
                btEliminar.setEnabled(true);
                try {
                    Utilitats.adabtarImatgeAlLabel(lbMapa, produccio.getCampId().getImg());
                } catch (IOException ex) {
                    Logger.getLogger(General.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (Collita.class.isInstance(value)) {
                //**********************************************************  COLLITA  *************************
                Collita collita = (Collita) value;
                out = Utilitats.getDescripcio(collita);
                btModificar.setEnabled(true);
                btPlantar.setEnabled(false);
                btCollir.setEnabled(false);
                btAbonar.setEnabled(false);
                btRuixar.setEnabled(false);
                btEliminar.setEnabled(true);
                tbtAcabada.setEnabled(false);
                try {
                    Utilitats.adabtarImatgeAlLabel(lbMapa, collita.getProduccioId().getCampId().getImg());
                } catch (IOException ex) {
                    Logger.getLogger(General.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (Productor.class.isInstance(value)) {
                //**********************************************************  PRODUCTOR  *************************
                Productor productor = (Productor) value;
                out = Textos.DESCRIPCIO_CAMP_NOM + productor.getNom();
                btModificar.setEnabled(false);
                btPlantar.setEnabled(false);
                btCollir.setEnabled(false);
                btAbonar.setEnabled(false);
                btRuixar.setEnabled(false);
                tbtAcabada.setEnabled(false);
                btEliminar.setEnabled(false);
            } else if (Abonat.class.isInstance(value)) {
                //**********************************************************  ABONAT  *************************
                Abonat abonat = (Abonat) value;
                out = Utilitats.getDescripcio(abonat) + "\n" + Utilitats.getDescripcio(abonat.getAbonoId());
                btModificar.setEnabled(true);
                btPlantar.setEnabled(false);
                btCollir.setEnabled(false);
                btAbonar.setEnabled(false);
                btRuixar.setEnabled(false);
                tbtAcabada.setEnabled(false);
                btEliminar.setEnabled(true);
            } else if (Tractament.class.isInstance(value)) {
                //**********************************************************  TRACTAMENT  *************************
                Tractament tractament = (Tractament) value;
                out = Utilitats.getDescripcio(tractament) + "\n" + Utilitats.getDescripcio(tractament.getFitosanitariId());
                btModificar.setEnabled(true);
                btPlantar.setEnabled(false);
                btCollir.setEnabled(false);
                btAbonar.setEnabled(false);
                btRuixar.setEnabled(false);
                tbtAcabada.setEnabled(false);
                btEliminar.setEnabled(true);
            }else if (String.class.isInstance(value)) {
                String selected = (String) value;
                if(value.equals(Textos.GENERAL_TREE_ABONATS)){
                    btAbonar.setEnabled(true);
                }else if(value.equals(Textos.GENERAL_TREE_COLLITES)){
                    btCollir.setEnabled(true);
                }else if(value.equals(Textos.GENERAL_TREE_TRACTAMENTS)){
                    btRuixar.setEnabled(true);
                }
                    
            }else{
                //**********************************************************  UNKNOWN  *************************
                out = "";
                btModificar.setEnabled(false);
                btPlantar.setEnabled(false);
                btCollir.setEnabled(false);
                btAbonar.setEnabled(false);
                btRuixar.setEnabled(false);
                btEliminar.setEnabled(false);
                tbtAcabada.setEnabled(false);
                lbMapa.setIcon(null);
            }
            taDescripcio.setText(out);
        } else {
            taDescripcio.setText("");
            btModificar.setEnabled(false);
            btPlantar.setEnabled(false);
            btCollir.setEnabled(false);
            btAbonar.setEnabled(false);
            btRuixar.setEnabled(false);
            btEliminar.setEnabled(false);
            tbtAcabada.setEnabled(false);
            lbMapa.setIcon(null);
        }

    }//GEN-LAST:event_treeValueChanged

    private void tbtAcabadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbtAcabadaActionPerformed
        Object object = ((DefaultMutableTreeNode) tree.getLastSelectedPathComponent()).getUserObject();
        if ((object != null) && (Produccio.class.isInstance(object))) {
            Produccio produccio = (Produccio) object;
            produccio.setTerminado(tbtAcabada.isSelected());
            try {
                produccions.edit(produccio);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, Textos.GENERAL_ERROR_NO_CABADA, "", JOptionPane.ERROR_MESSAGE);
                tbtAcabada.setSelected(!tbtAcabada.isSelected());
            }
            update(null, null);
        }
    }//GEN-LAST:event_tbtAcabadaActionPerformed

    private void btModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btModificarActionPerformed
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (node != null) {
            Object value = node.getUserObject();
            if (Produccio.class.isInstance(value)) {
                Produccio produccio = (Produccio) value;
                if(plantar == null){
                    plantar = new Plantar(this, produccio,null);
                }else{
                    plantar.update(null, produccio);
                }
                plantar.setVisible(true);
                this.setVisible(false);
            } else if (Collita.class.isInstance(value)) {
                Collita collita = (Collita) value;
                if (!collita.getProduccioId().getTerminado()) {
                    if(collir== null){
                        collir = new Collir(this, collita, null);
                    }else{
                        collir.update(null,collita);
                    }
                    collir.setVisible(true);
                    this.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(this, Textos.GENERAL_ERROR_NO_ALEMINAR_PERKE_ACABADA, "", JOptionPane.ERROR_MESSAGE);
                }
            }else if (Abonat.class.isInstance(value)){
                Abonat abonat = (Abonat) value;
                if(abonar == null){
                    abonar = new Abonar(this,abonat, null);
                }else{
                    abonar.update(null, abonat);
                }
                abonar.setVisible(true);
                this.setVisible(false);
            }else if (Tractament.class.isInstance(value)){
                Tractament tractament = (Tractament) value;
                if(tractar == null){
                    tractar = new Tractar(this,tractament,null);
                }else{
                    tractar.update(null, tractament);
                }
                tractar.setVisible(true);
                this.setVisible(false);
            }
        }
    }//GEN-LAST:event_btModificarActionPerformed

    private void btEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEliminarActionPerformed
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (node != null) {
            Object value = node.getUserObject();
            if (Produccio.class.isInstance(value)) {
                Produccio produccio = (Produccio) value;
                int i = JOptionPane.showConfirmDialog(this, Textos.GENERAL_WARNING_ELIMINAR_PRODUCCIO, "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                if (i == 0) {
                    try {
                        Collection<Collita> collitesElim = produccio.getCollitaCollection();
                        for (Collita collita : collitesElim) {
                            collites.destroy(collita.getId());
                        }
                        Thread.sleep(500);
                        produccions.destroy(produccio.getId());
                        update(null, null);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(General.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NonexistentEntityException ex) {
                        JOptionPane.showMessageDialog(this, Textos.COMU_ERROR_BASE_DADES, "", JOptionPane.ERROR_MESSAGE);
                    } catch (IllegalOrphanException ex) {
                        Logger.getLogger(General.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

            } else if (Collita.class.isInstance(value)) {
                Collita collita = (Collita) value;
                if (!collita.getProduccioId().getTerminado()) {
                    int i = JOptionPane.showConfirmDialog(this, Textos.GENERAL_WARNING_ELIMINAR_COLLITA, "", JOptionPane.WARNING_MESSAGE);
                    if (i == 0) {
                        try {
                            collites.destroy(collita.getId());
                            update(null, null);
                        } catch (NonexistentEntityException ex) {
                            JOptionPane.showMessageDialog(this, Textos.COMU_ERROR_BASE_DADES, "", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                } else {
                    JOptionPane.showMessageDialog(this, Textos.GENERAL_ERROR_NO_ALEMINAR_PERKE_ACABADA, "", JOptionPane.ERROR_MESSAGE);
                }
            }else if (Abonat.class.isInstance(value)){
                Abonat abonat = (Abonat) value;
                int i = JOptionPane.showConfirmDialog(this, Textos.GENERAL_WARNING_ELIMINAR_ABONAT, "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                if (i == 0) {
                    try {
                        abonats.destroy(abonat.getId());
                        update(null, null);
                    } catch (NonexistentEntityException ex) {
                        Logger.getLogger(General.class.getName()).log(Level.SEVERE, null, ex);
                        JOptionPane.showMessageDialog(this, Textos.COMU_ERROR_BASE_DADES, "", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }else if (Tractament.class.isInstance(value)){
                Tractament tractament = (Tractament) value;
                int i = JOptionPane.showConfirmDialog(this, Textos.GENERAL_WARNING_ELIMINAR_TRACTAMENT, "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                if (i == 0) {
                    try {
                        tractaments.destroy(tractament.getId());
                        update(null, null);
                    } catch (NonexistentEntityException ex) {
                        Logger.getLogger(General.class.getName()).log(Level.SEVERE, null, ex);
                        JOptionPane.showMessageDialog(this, Textos.COMU_ERROR_BASE_DADES, "", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }
    }//GEN-LAST:event_btEliminarActionPerformed

    private void lbMapaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbMapaMouseClicked
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (node != null) {
            Object value = node.getUserObject();
            if (Produccio.class.isInstance(value)) {
                Produccio produccio = (Produccio) value;
                Camp camp = produccio.getCampId();
                List<Produccio> veins = produccions.findProduccioEntitiesByCamp(camp, false);
                veins.remove(produccio);
                new DialogSituarParcela(this, true, camp, produccio, veins, false).setVisible(true);
            } else if (Camp.class.isInstance(value)) {
                Camp camp = (Camp) value;
                List<Produccio> produccionsVeines = produccions.findProduccioEntitiesByCamp(camp, false);
                new DialogSituarParcela(this, true, camp, null, produccionsVeines, false).setVisible(true);
            }
        }
    }//GEN-LAST:event_lbMapaMouseClicked

    private void btAbonarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAbonarActionPerformed
        Produccio produccio = (Produccio)((DefaultMutableTreeNode)tree.getLastSelectedPathComponent()).getUserObject();
        if(abonar == null){
            abonar = new Abonar(this, null, produccio);
        }else{
            abonar.update(null, produccio);
        }
        abonar.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btAbonarActionPerformed

    private void btPlantarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPlantarActionPerformed
        Camp camp = (Camp)((DefaultMutableTreeNode)tree.getLastSelectedPathComponent()).getUserObject();
        if(plantar == null){
            plantar = new Plantar(this, null,camp);
        }else{
            plantar.update(null, camp);
        }
        plantar.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btPlantarActionPerformed

    private void btCollirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCollirActionPerformed
        Produccio produccio = (Produccio)((DefaultMutableTreeNode)tree.getLastSelectedPathComponent()).getUserObject();
        if(!Utilitats.compleixPlazSeguretat(produccio)){
            JOptionPane.showMessageDialog(this, Textos.GENERAL_WARNING_COLLITA_PLAZ, "", JOptionPane.WARNING_MESSAGE);
        }
        if(collir == null){
            collir = new Collir(this, null, produccio);
        }else{
            collir.update(null, produccio);
        }
        collir.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btCollirActionPerformed

    private void btRuixarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRuixarActionPerformed
        Produccio produccio = (Produccio)((DefaultMutableTreeNode)tree.getLastSelectedPathComponent()).getUserObject();
        if(tractar == null){
            tractar = new Tractar(this, null, produccio);
        }else{
            tractar.update(null, produccio);
        }
        tractar.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btRuixarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgVisualitzar;
    private javax.swing.JButton btAbonar;
    private javax.swing.JButton btCollir;
    private javax.swing.JButton btEliminar;
    private javax.swing.JButton btMenu;
    private javax.swing.JButton btModificar;
    private javax.swing.JButton btPlantar;
    private javax.swing.JButton btRuixar;
    private javax.swing.JComboBox cbProductor;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbMapa;
    private javax.swing.JLabel lbProductor;
    private javax.swing.JRadioButton rbSenseAcabar;
    private javax.swing.JRadioButton rbTot;
    private javax.swing.JTextArea taDescripcio;
    private javax.swing.JToggleButton tbtAcabada;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables

    private void update_Tree() {
        Productor productor = (Productor) cbProductor.getSelectedItem();
        if (productor != null) {
            lastPath = tree.getSelectionPath();
            if (rbTot.isSelected()) {
                tree.setModel(Utilitats.getTreeModel(productor, true));
            } else if (rbSenseAcabar.isSelected()) {
                tree.setModel(Utilitats.getTreeModel(productor, false));
            }
            for (Enumeration e = ((TreeNode) tree.getModel().getRoot()).children(); e.hasMoreElements();) {
                TreeNode tn = (TreeNode) e.nextElement();
                tree.expandPath(new TreePath(((DefaultTreeModel) tree.getModel()).getPathToRoot(tn)));
            }
            if(lastPath != null){
                tree.clearSelection();
                tree.expandPath(lastPath.getParentPath());
                tree.scrollPathToVisible(lastPath);
                tree.addSelectionPath(lastPath);
            }
        }
    }

    @Override
    public void update(Observable o, Object o1) {
        update_Tree();
    }

    @Override
    public void setVisible(boolean bln) {
        if(bln){
            update(null, null);
        }
        super.setVisible(bln);
    }
}
